<?php
function export_create_sql($data,$save_type,$dbname){
	switch($save_type):
		//-----------------------
		//     保存为HTML文件
		//-----------------------
		case "html":
			header("Content-type:application/html;charset=utf-8");
			header('Content-Disposition: attachment; filename="'.$dbname.'.html"');
			$content="";
			$content.= '<div style="background-color:#f2f2f2;height:80px;line-height:80px;padding-left:20px;font-size:16px;">数据库【'.$dbname."】创建语句 - 创建自 MayaTools工具包 的  DataBaseStructExport. 创建日期： ".date("Y-m-d H:i:s")."</div>\n";
			for($i=0;$i<count($data);$i++):
				if($data[$i]['Table']!=""):
					$content.= '<div class="tb_title" style="margin-top:10px;">'.$data[$i]['Table']."</div>\n";
					$content.= '<div><textarea style="width:100%;height:100px;border:0px none;">'.$data[$i]['Create Table']."</textarea></div>\n";
				endif;
			endfor;
			$fname="tpl_create_sql.html";
			$fp=fopen($fname,"r");
			$html=fread($fp,filesize($fname));
			fclose($fp);
			$html=str_replace("{#DATA_CONTENT}",$content,$html);
			echo $html;
			break;
			
		//-----------------------
		//     保存为TXT文件
		//-----------------------
		case "txt":
			header("Content-type:application/txt;charset=utf-8");
			header('Content-Disposition: attachment; filename="'.$dbname.'.txt"');
			$content="";
			$content.= "/*\r\n数据库【".$dbname."】创建语句 \r\n创建自 MayaTools工具包 的  DataBaseStructExport. \r\n创建日期： ".date("Y-m-d H:i:s")." \r\n*/\r\n\r\n";
			for($i=0;$i<count($data);$i++):
				if($data[$i]['Table']!=""):
					$content.= "/*表：".$data[$i]['Table']."*/\n";
					$content.="DROP TABLE IF EXISTS `".$data[$i]['Table']."`;";
					$content.= "\n".$data[$i]['Create Table'].";\r\n\r\n";
				endif;
			endfor;
			echo $content;
			break;
			
		//-----------------------
		//     保存为CSV文件
		//-----------------------
		case "csv":
			header("Content-type:application/csv;charset=gbk");
			header('Content-Disposition: attachment; filename="'.$dbname.'.csv"');
			$content="";
			for($i=0;$i<count($data);$i++):
				$content.= '字段名称';
				$content.= ",".'数据类型';
				$content.= ",".'键';
				$content.= ",".'默认值';
				$content.= ",".'允许为空';
				$content.= ",".'注释'."\n";
				foreach($data[$i]['columns'] as $column):
					$content.= $column['COLUMN_NAME'];
					$content.= ",".$column['COLUMN_TYPE'];
					$content.= ",".$column['COLUMN_KEY'];
					$content.= ",".$column['COLUMN_DEFAULT'];
					$content.= ",".$column['IS_NULLABLE'];
					$content.= ",".$column['COLUMN_COMMENT']."\n";
				endforeach;
				$content.= "\n\r";
			endfor;
			echo iconv("utf-8","gbk",$content);
			break;
	endswitch;
}